package com.gst.handfood.controller;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.gst.handfood.R;
import com.gst.handfood.utils.UIUtils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * Created by YBH on 2017/10/31.
 */

public abstract class LoadingPage extends FrameLayout{
    private Context mContext;
    //四种加载布局
    private View loadingView;
    private View errorView;
    private View emptyView;
    private View successView;

    //四种状态
    private static final int LOADING_STATE = 1;
    private static final int ERROR_STATE = 2;
    private static final int EMPTY_STATE = 3;
    private static final int SUCCESS_STATE = 4;

    //默认状态为正在加载
    private int CURRENT_STATE = 1;
    private ViewGroup.LayoutParams lp;
    private AsyncHttpClient client;

    //拿到枚举的状态结果
    private  ResultState resultState;
    public LoadingPage(@NonNull Context context) {
        this(context,null);
    }

    public LoadingPage(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LoadingPage(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;//上下文一定的保存记录 不然后续再清单文件中调用样式文件会不显示
        //初始化布局
        initView();
    }

    private void initView() {
        //自定义布局参数
        lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        if (loadingView==null) {
            loadingView = UIUtils.getXmlView(R.layout.page_loading);
            addView(loadingView, lp);
        }
        if (errorView==null) {
            errorView = UIUtils.getXmlView(R.layout.page_error);
            addView(errorView, lp);
        }
        if (emptyView==null) {
            emptyView = UIUtils.getXmlView(R.layout.page_empty);
            addView(emptyView, lp);
        }
        //如果是成功的页面，这里让强制子类实现（确保是主线程）
        ShowSafeView();

    }

    private void ShowSafeView() {
        UIUtils.runOnUIThread(new Runnable() {
            @Override
            public void run() {
                showSuccessView();
            }
        });
    }

    private void showSuccessView() {
        //隐藏其他三个页面
        loadingView.setVisibility(CURRENT_STATE==LOADING_STATE?View.VISIBLE:View.INVISIBLE);
        errorView.setVisibility(CURRENT_STATE==ERROR_STATE?View.VISIBLE:View.INVISIBLE);
        emptyView.setVisibility(CURRENT_STATE==EMPTY_STATE?View.VISIBLE:View.INVISIBLE);

        //1子类去实现顺序不能调1
        if(successView==null) {
            successView = View.inflate(mContext,LayoutId(),null);
            addView(successView,lp);
        }
        //2显示successView2
        successView.setVisibility(CURRENT_STATE==SUCCESS_STATE?View.VISIBLE:View.INVISIBLE);
    }

    public abstract int LayoutId();

    /**
     * 封装网络请求 最终显示
     */
    public void show(){
        //加载状态要归位
        if (CURRENT_STATE!=LOADING_STATE) {
            CURRENT_STATE = LOADING_STATE;
        }
        client = new AsyncHttpClient();
        //处理不需要发送网络请求
        String url = url();
        if (TextUtils.isEmpty(url)) {
            resultState=ResultState.SUCCESS;
            resultState.setContent("");
            loadView();
        }
        client.post(url,params(),new AsyncHttpResponseHandler(){
            @Override
            public void onSuccess(String content) {
                if (TextUtils.isEmpty(content)) {
                    resultState = ResultState.EMPTY;
                    resultState.setContent("");
                } else {
                    resultState = ResultState.SUCCESS;
                    resultState.setContent(content);
                }
                //改变状态
                loadView();
            }

            @Override
            public void onFailure(Throwable error, String content) {
                resultState = ResultState.ERROR;
                resultState.setContent("");
                loadView();
            }
        });
    }
    //加载页面 动态改变当前加载状态
    private void loadView() {
        //判断 遍历枚举 设置当前
        switch (resultState) {
            case ERROR:
                CURRENT_STATE = 2;
                break;
            case EMPTY:
                CURRENT_STATE = 3;
                break;
            case SUCCESS:
                CURRENT_STATE = 4;
                break;
        }
        //显示页面
        ShowSafeView();
        //如果当前是成功状态 让子类实现 把枚举状态和view传进去
        if (CURRENT_STATE==4) {
            onSuccess(resultState,successView);
        }

    }

    //子类实现 成功后的显示
    protected abstract void onSuccess(ResultState resultState, View successView);

    //子类传url
    protected abstract String url();

    //子类实现请求参数方法
    protected abstract RequestParams params();

    //把加载状态的结果封装到枚举中
    public enum ResultState{
        ERROR(2),EMPTY(3),SUCCESS(4);
        private String content;
        private int state;

        ResultState(int state) {
            this.state = state;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public int getState() {
            return state;
        }

        public void setState(int state) {
            this.state = state;
        }
    }

}
